<template>
    <div class="productlist-page">
        <product-nav />
        <div class="search-input">
            <van-search
                @search="enterSearch"
                v-model="liststate.searchmodel.key"
                :placeholder="$t('chan-pin-ming-cheng-chan-pin-xing-hao')" />
        </div>
        <div class="search-labels">
            <ul class="tag-list">
                <li
                    class="tag-item"
                    :class=" tagsstate.tagsmap[child.name] ? 'active': '' "
                    @click="toggleSelectTag(child)"
                    v-for="child in liststate.labels"
                    :key="child.id">
                    {{ child.name }}
                </li>
            </ul>
        </div>
        <div class="search-results">
            <van-list
                v-model:loading="liststate.loading"
                :finished="liststate.isfinished"
                :finished-text="$t('mei-you-geng-duo-le')"
                @load="getProductList">
                <ul class="product-list">

                    <li
                        class="product-list-item"
                        @click="toToProductPage(child)"
                        v-for="child in liststate.productlist"
                        :key="child.key">
                        <img
                            :style="getCoverImageHeight()"
                            :src="getCoverpicture(child)"
                            alt=""
                            srcset="">

                        <div class="product-name"> {{ child.name }} </div>
                        <div class="product-model"> {{$t('xing-hao')}} {{ child.model }} </div>
                    </li>
                </ul>
            </van-list>

        </div>
    </div>
</template>

<script>
import {
    useProductSetup
} from '@/setup/product/ProductSetup';
import {
    onMounted, reactive
} from '@vue/runtime-core';
import ProductNav from './common/ProductNav.vue';
import { useRoute, useRouter, onBeforeRouteUpdate } from 'vue-router';
export default {
    setup () {
        const {
            getProductLabels,
            getProductList,
            getCoverpicture,
            liststate
        } = useProductSetup();

        const tagsstate = reactive({
            tagsmap: {}
        });
        const router = useRouter();

        const setQuery = () => {
            router.push({
                path: '/productlist',
                query: {
                    key: liststate.searchmodel.key,
                    tags: liststate.searchmodel.tags.join(',')
                }
            });
        };
        const getCoverImageHeight = () => {
            const width = document.body.clientWidth * 0.5 - 10 - 32;
            return {
                height: width + 'px'
            };
        };
        const toggleSelectTag = (tag) => {
            tagsstate.tagsmap[tag.name] = !tagsstate.tagsmap[tag.name];
            liststate.searchmodel.tags = Object.keys(tagsstate.tagsmap).filter((key) => {
                return tagsstate.tagsmap[key];
            });
            liststate.searchmodel.p = 1;
            getProductList();
            setQuery();
        };

        const enterSearch = (value) => {
            liststate.searchmodel.p = 1;
            liststate.searchmodel.key = value;

            getProductList();
            setQuery();
        };

        const toToProductPage = (child) => {
            router.push({
                path: '/productdetail',
                query: {
                    productid: child.product_id
                }
            }
            );
        };

        const routeChange = () => {
            const route = useRoute();
            const query = route.query;
            liststate.searchmodel = {
                ...liststate.searchmodel,
                key: query.key,
                tags: query.tags ? query.tags.split(',') : []
            };

            const tagsmap = {};
            liststate.searchmodel.tags.forEach((child) => {
                tagsmap[child] = true;
            });

            tagsstate.tagsmap = tagsmap;

            getProductLabels();
            getProductList();
        };

        onMounted(() => {
            routeChange();
        });
        onBeforeRouteUpdate(async (to, from) => {
            routeChange();
        });
        return {
            liststate,
            tagsstate,
            enterSearch,
            getCoverImageHeight,
            getCoverpicture,
            toToProductPage,
            toggleSelectTag,
            getProductLabels,
            getProductList
        };
    },
    components: {
        ProductNav
    },
    data () {
        return {

        };
    }
};

</script>

<style lang="scss" scoped>
    .productlist-page {
        padding-top: 44px;
        background-color: #f0f0f0;
        min-height: 100%;
        .search-input {
            .van-search {
                background: #ededed;
                box-shadow: 0px 1px 0px 0px #dddddd;
            }
        }

        .search-labels {
            padding: 14px 10px;

            .tag-list {
                display: flex;
                flex-wrap: wrap;

                .tag-item {
                    height: 25px;
                    background: #ffffff;
                    border-radius: 13px;
                    padding: 0 16px;
                    line-height: 25px;
                    font-size: 13px;
                    margin-bottom:5px;
                    margin-right: 10px;
                    &.active {
                        background: #61CFBE;
                        color:#ffffff
                    }
                }
            }
        }

        .product-list {
            display: flex;
            padding: 0 5px;
            flex-wrap: wrap;

            .product-list-item {
                list-style: none;
                background: #ffffff;
                border-radius: 8px;
                width: calc(50% - 10px);
                padding: 16px;
                margin: 5px;
                box-sizing: border-box;
                img {
                    width: 100%;
                    height: 140px;
                }

                .product-name {
                    font-size: 15px;
                    color: #000;
                    margin-top: 15px;
                    margin-bottom: 5px;
                        width: 100%;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        white-space: nowrap;
                }

                .product-model {
                    font-size: 13px;
                    color: rgba(0, 0, 0, 0.6);
                    width: 100%;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                }
            }
        }
    }

</style>
